package db.seed;

import com.mai4j.maiadmin.setup.Helper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;

import java.math.BigDecimal;
import java.time.LocalDateTime;

import static com.mai4j.maiadmin.generated.Tables.CURRENCY;
import static com.mai4j.maiadmin.generated.Tables.CURRENCY_DESCRIPTION;
import static org.jooq.impl.DSL.using;


/**
 * 初始化国家和地区
 */
@Slf4j
public class V20230409165659__Insert_Currency extends BaseJavaMigration {

    @SneakyThrows
    @Override
    public void migrate(Context context) {

        long currencyId = Helper.next();
        using(context.getConnection())
                .insertInto(CURRENCY)
                .columns(
                        CURRENCY.CURRENCY_ID,
                        CURRENCY.CODE,
                        CURRENCY.SYMBOL_LEFT,
                        CURRENCY.SYMBOL_RIGHT,
                        CURRENCY.DECIMAL_PLACE,
                        CURRENCY.VALUE,
                        CURRENCY.SORT_ORDER,
                        CURRENCY.STATUS,
                        CURRENCY.DATE_ADDED,
                        CURRENCY.DATE_MODIFIED
                )
                .values(currencyId, "CNY", "￥", "", 4, BigDecimal.ONE, 0, (byte) 1, LocalDateTime.now(), LocalDateTime.now()).execute();
        using(context.getConnection())
                .insertInto(CURRENCY_DESCRIPTION)
                .columns(
                        CURRENCY_DESCRIPTION.CURRENCY_ID,
                        CURRENCY_DESCRIPTION.LANGUAGE_CODE,
                        CURRENCY_DESCRIPTION.NAME
                )
                .values(currencyId, "zh-CN", "人民币")
                .values(currencyId, "en-US", "RMB")
                .execute();
    }
}
